package engine.logic.login
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.logic.ServerTimerLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import newx.com.config.StaticIni;

	/**
	 * 玩家CD时间逻辑类 
	 * @author wanghb
	 * 
	 */
	public class PlayCDTimerLogic
	{
//		CDType_SoloFB				= 1,		// 单人副本
//		CDType_PeerageProp			= 2,		// 爵位属性升级CD
//		CDType_EquipUpgrade			= 3,		// 爵位属性升级CD
//		CDType_GuildThesaurus		= 4,		// 家族宝库领取CD
//		CDType_Practise				= 5,		// 练功
//		CDType_FRIEND_PRODUCTION	= 6,		// 产业
//		CDType_ConvoyRob			= 7,		// 劫镖CD
//		CDType_Convoy				= 8,		// 运镖CD
//		CDType_Lobby				= 9,		// 游说cd
//		CDType_WorldTravel			= 10,		// 游历江湖
//		CDType_WorldBossRelive		= 11,		// 世界Boss复活计时
//		CDType_Pklise				= 12,		// 排名经济场
		
//		CDType_getFamily				= 17,		// 招募帮众
		
		private var _timer			:Timer;
		private var _bindManage	:BindManage;
		private var _model			:ModelLocator
		/**注册类型函数数组*/
		private var _typeFunArray	:Array
		/**注册全队列函数数组*/
		private var _typeAllFunArray:Array;
		/**类型对象容器*/
		private var _typeRec		:Object
		/**是否在记时*/
		private var _isStarTimer	:Boolean	=	false;
		/**是否获取过服务器时间*/
		private var _getServerTime	:Boolean	=	false;
		/**服务器时间*/
		private var _serverTime	:int;
		/**用来检查最小时间的数组对列*/
		private var _checkMinTimeArray:Array;
		/**回调函数*/
		private var _callFunction	:Function
		/**是否停止记时*/
		private var _isStop:Boolean			=	false;
		private static var _self	:PlayCDTimerLogic
		
		/**装备强化*/
		private static var EQUIP_UP:int	=	3;
		
		/**用于标示是否能够请求服务器时间，方式连续请求*/
		private var canCallSeverTime:Boolean	=	true;
			
			
		public function PlayCDTimerLogic()
		{
			_typeRec			=	{};
			_typeFunArray		=	[];
			_typeAllFunArray	=	[];
			_bindManage			=	BindManage.getInstance();
			_model				=	ModelLocator.getInstance();
			_timer				=	new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER, timerHandle);
			_bindManage.BindRecord(_model.scene,"PlayerCDRec",addRec,removeRec);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SENDSERVERTIME),getServerTime);
		}
		
		public static function getInstance():PlayCDTimerLogic
		{
			if(_self == null)
			{
				_self	=	new PlayCDTimerLogic();
			}
			return _self;
		}
		/**
		 * 注册函数 
		 * @param type 类型 
		 * @param fun  回调函数，返回时间队列中最小时间的
		 * 
		 * functin (val:int ，col:int):void
		 * 
		 */		
		public function resgis(type:int,fun:Function):int
		{
			var arrayIndex:int;
			if(_typeFunArray[type] == null)
			{
				_typeFunArray[type]	=	[];
			}
			var tempLenght:int	=	(_typeFunArray[type] as Array).length;
			
			for(var i:int= 0; i < tempLenght; i++)
			{
				if(_typeFunArray[type][i] == null)
				{
					_typeFunArray[type][i]	=	fun;
					arrayIndex			=	i;
					return arrayIndex;
				}
			}
			
			(_typeFunArray[type] as Array).push(fun);
			arrayIndex	=	(_typeFunArray[type] as Array).length;
			return arrayIndex;
		}
		/**
		 * 删除注册函数 
		 * @param type  类型
		 * @param index 索引
		 * 
		 */		
		public function removeResgin(type:int,index:int):void
		{
			if(_typeFunArray[type] != null)
			{
				_typeFunArray[type][index] = null;
			}
		}
		/**
		 * 注册全队列返回函数
		 * @param type 类型 
		 * @param fun  回调函数，返回全队列的时间
		 * @param  functin (val:int ，[col:int]Array):void
		 * @param 
		 * 
		 */		
		public function resgisAll(type:int,fun:Function):int
		{
			var arrayAllIndex:int;
			if(_typeAllFunArray[type] == null)
			{
				_typeAllFunArray[type]	=	[];
			}
			var tempLenght:int	=	(_typeAllFunArray[type] as Array).length;
			
			for(var i:int= 0; i < tempLenght; i++)
			{
				if(_typeAllFunArray[type][i] == null)
				{
					_typeAllFunArray[type][i]	=	fun;
					arrayAllIndex			=	i;
					return arrayAllIndex;
				}
			}
			
			(_typeAllFunArray[type] as Array).push(fun);
			arrayAllIndex	=	(_typeAllFunArray[type] as Array).length;
			return arrayAllIndex;
		}
		/**
		 *  
		 * 删除全队列返回注册函数 
		 * @param type  类型
		 * @param index 索引
		 * 
		 */		
		public function removeResgisAll(type:int,index:int):void
		{
			if(_typeAllFunArray[type] != null)
			{
				_typeAllFunArray[type][index] = null;
			}
		}
		/**
		 * 添加表数据 
		 * @param PropertiesName
		 * @param val
		 * @param data
		 */		
		private function addRec(PropertiesName:String,val:*,data:*):void
		{
			if(_typeRec[val[0]] == null)
			{
				_typeRec[val[0]]	=	{};
			}
			_typeRec[val[0]][val[1]]	=	{col:val[1],timer:val[2],data:val};
			
			if(!_isStarTimer)
			{
				if(canCallSeverTime)
				{
					trace("call time");
					canCallSeverTime	=	false;
					setTimeout(function():void
					{
						trace("call time setTimeout");
						canCallSeverTime	=	true;
						SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);
					},5000);
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);
				}
			}
			else
			{
				timerHandle(null);
			}
		}
		/**
		 * 删除表数据 
		 * @param PropertiesName
		 * @param val
		 * @param data
		 * 
		 */		
		private function removeRec(PropertiesName:String,val:*,data:*):void
		{
			//这个现在不可能出现的，有的时候再做吧
		}
		
		private function getServerTime(e:CustomMsgEvent):void
		{
			_serverTime	=	e.pList[0];
			_isStarTimer	=	true;
			_timer.start();
		}
		/**
		 * 计时器 
		 * @param e
		 * 
		 */		
		private function timerHandle(e:TimerEvent):void
		{
			
			_isStop			=	true;
			var minTime:int	= 0;
			var timerArray:Array	=	[];
			for(var val:String in _typeRec)
			{
				var itemObj:Object	=	_typeRec[val];
				if(itemObj != null)
				{
					_checkMinTimeArray	=	[];
					for(var itemStr:String	in itemObj)
					{
						if(itemObj[itemStr] != null)
						{	
							minTime	=	itemObj[itemStr]["timer"] - ServerTimerLogic.getInstance().getServerTimer();
							if(minTime > 0)
							{
								_isStop	=	false;
							}
							itemObj[itemStr]["haveTimer"] = minTime <= 0 ? 0 : minTime;
							_checkMinTimeArray.push( itemObj[itemStr]);
						}
						_checkMinTimeArray.sortOn("haveTimer");
					}
					//回调当前类型的
					if(_typeFunArray[val] != null)
					{
						for(var i:int =0; i < (_typeFunArray[val]as Array).length;i++)
						{
							_callFunction	=	(_typeFunArray[val]as Array)[i] as Function;
							if(_callFunction != null)
							{
								_callFunction.call(this,_checkMinTimeArray[0]["haveTimer"],_checkMinTimeArray[0]["col"]);
							}
						}
					}
					//返回全队列类型的
					if(_typeAllFunArray[val] != null)
					{
						for(var j:int =0; j < (_typeAllFunArray[val]as Array).length;j++)
						{
							_callFunction	=	(_typeAllFunArray[val]as Array)[j] as Function;
							if(_callFunction != null)
							{
								for(var m:int = 0; m <  _checkMinTimeArray.length; m++)
								{
									_callFunction.call(this,_checkMinTimeArray[m]["haveTimer"],_checkMinTimeArray[m]["data"]);
								}
							}
						}
					}
				}
			}
//			if(e != null)
//			{
//				_serverTime ++;
//			}
			//停止计时器
			if(_isStop)
			{
				_isStarTimer	=	false;
//				_getServerTime	=	false;
				_timer.stop();
			}
		}
		/**
		 * 根据秒数来返回时间格式字符串  
		 * @param val
		 * @param isHours
		 * @return 
		 * 
		 */		
		public function getTimerStr(val:int,isHours:Boolean = false):String
		{
			var timeStr:String	=	"";
			var s:int	=	0;
			var f:int	=	0;
			var m:int	=	0;
			
			var ss:String	=	"";
			var fs:String	=	"";
			var ms:String	=	"";
			if(val >0)
			{
				s	=	Math.floor(val/3600);
				f	=	Math.floor(val%3600/60);
				m	=	val - 3600 * s - 60 * f;
			}
			ss	=	s < 10 ? "0" + String(s):String(s);
			fs	=	f < 10 ? "0" + String(f):String(f);
			ms	=	m < 10 ? "0" + String(m):String(m);
			
			if(isHours)
			{
//				timeStr	=	StaticIni.getSwfLangStrVar()
				timeStr	=	ss + ":" + fs +":"+ ms;
			}
			else
			{
				timeStr	=	fs +":"+ ms;
			}
			return timeStr;
		}
		
		/**
		 * 根据秒数来返回中文时间格式字符串  最大单位天
		 * @param val  单位：秒
		 * @return  
		 * 
		 */		
		public function getTimerStr1(val:int):String
		{
			var timeStr:String	=	"";
			var t:int	=   0;
			var s:int	=	0;
			var f:int	=	0;
			var m:int	=	0;
			
			var ts:String	=	"";
			var ss:String	=	"";
			var fs:String	=	"";
			var ms:String	=	"";
			if(val >0)
			{
				t	= 	Math.floor(val/24/3600)
				s	=	Math.floor(val%24/3600);
				f	=	Math.floor(val%3600/60);
				m	=	val - 3600 * s - 60 * f;
			}
			ts	= 	t < 10 ? "" + String(t):String(t);
			ss	=	s < 10 ? "" + String(s):String(s);
			fs	=	f < 10 ? "" + String(f):String(f);
			ms	=	m < 10 ? "" + String(m):String(m);
			
			if(ts == "0")
			{
				timeStr	=ss + "时" + fs +"分"+ ms+"秒";
			}
			else 
			{
				timeStr	=StaticIni.getSwfLangStrVar("UIString_1000678",[ts,ss,fs])//天  时  分
			}
			return timeStr;
		}
		/**
		 * 刷新
		 */		
		public function upData():void
		{
			timerHandle(null);
		}
	}
}